/*****************************************************************************
 *
 *                      HUSONG PROPRIETARY INFORMATION
 *
 *          The information contained herein is proprietary to HUSONG
 *           and shall not be reproduced or disclosed in whole or in part
 *                    or used for any design or manufacture
 *              without direct written authorization from HUSONG.
 *
 *            Copyright (c) 2015 by HUSONG.  All rights reserved.
 *
 *****************************************************************************/
package me.send.business.service.mapper.provider;

import java.util.Map;

import me.send.framework.util.LoggerUtils;

/**
 * @author maxmin
 *
 * @date 2015年9月1日 下午5:35:29
 */
public class BrokerListProvider {
	
	private final static int  SORT_TYPE_DEFAULT=1;
	private final static int  SORT_TYPE_GOOD_EVALUATE=2;
	private final static int  SORT_TYPE_LEVEL=3;
	private final static int  SORT_TYPE_DESTANCE=4;
	
	public String getBrokerList(Map<String, Object> param) {
		int type = (Integer)param.get("type");
		int orderid = (Integer)param.get("orderid");
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT ob.order_broker_id,ob.broker_id,ob.order_id,ob.ischecked,ob.latitude,ob.longitude,broker.username,")
		.append(" (SELECT DISTINCT IF(COUNT(1)>0,SUM(be.point)/COUNT(be.point),0) FROM broker_evaluate be "
				+ "		LEFT JOIN orders o ON be.orderid = o.orderid"
				+ "		LEFT JOIN orderbroker odb ON odb.order_id = o.orderid"
				+ "		WHERE odb.broker_id = ob.broker_id AND odb.ischecked=1 "
				+ ") stars,")
		.append(" broker.`level`,"
				+ "(SELECT COUNT(1) FROM orderbroker obr LEFT JOIN orders o ON o.orderid = obr.order_id "
				+ "		WHERE obr.broker_id = ob.broker_id AND obr.ischecked = 1 AND o.`status`>=8"
				+ ") as orders,")
		.append(" (ROUND(2*ASIN(SQRT(POW(SIN((ob.latitude*PI()/180-o.latitude*PI()/180)/2),2)+COS(ob.latitude*PI()/180)*COS(o.latitude*PI()/180)*POW(((ob.longitude*PI()/180-o.longitude*PI()/180)/2),2)))*6378137.0*10000)/10000) destance,")
		.append(" broker.headerpath FROM orderbroker ob ")
		.append(" LEFT JOIN popuser broker ON ob.broker_id = broker.popid ")
		.append(" LEFT JOIN orders o ON ob.order_id = o.orderid ")
		.append(" WHERE order_id="+orderid);
		if (type == SORT_TYPE_DEFAULT) {
			sb.append(" ORDER BY  ob.order_broker_id ASC ");
		}
		if (type == SORT_TYPE_GOOD_EVALUATE) {
			sb.append(" ORDER BY  stars DESC ");
		}
		if (type == SORT_TYPE_LEVEL) {
			sb.append(" ORDER BY  level DESC ");
		}
		if (type == SORT_TYPE_DESTANCE) {
			sb.append(" ORDER BY  destance ASC ");
		}
		LoggerUtils.info(getClass().getName(), "str:"+sb.toString());
		return sb.toString();
	}
}
